package com.ajie.slimming.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.ajie.slimming.config.Constants;
import com.ajie.slimming.util.Util;

public class UserInfoHelper
{
    public final static String SEX = "sex";
    
    public final static String HEIGHT = "height";
    
    public final static String WEIGHT = "weight";
    
    public final static String AGE = "age";
    
    private SharedPreferences mSharedPreferences;
    
    private Editor editor;
    
    private static UserInfoHelper instance = null;
    
    public static UserInfoHelper getInstance()
    {
        if (instance == null)
        {
            instance = new UserInfoHelper();
        }
        return instance;
    }
    
    private UserInfoHelper()
    {
        mSharedPreferences = SlimmingApplication.getBPContext()
                .getSharedPreferences(Constants.PRE_USER_INFO,
                        Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }
    
    public boolean isInfoComplete()
    {
        String sex = mSharedPreferences.getString(UserInfoHelper.SEX, "");
        String height = mSharedPreferences.getString(UserInfoHelper.HEIGHT, "");
        String weight = mSharedPreferences.getString(UserInfoHelper.WEIGHT, "");
        String age = mSharedPreferences.getString(UserInfoHelper.AGE, "");
        return !TextUtils.isEmpty(sex) && !TextUtils.isEmpty(height)
                && !TextUtils.isEmpty(weight) && !TextUtils.isEmpty(age);
    }
    
    public double getStandardWeight()
    {
        String sex = mSharedPreferences.getString(UserInfoHelper.SEX, "");
        String height = mSharedPreferences.getString(UserInfoHelper.HEIGHT, "");
        return isInfoComplete() ? Util.getStandardWeight(sex, height) : 0;
    }
    
    public double getTarget()
    {
        String sex = mSharedPreferences.getString(UserInfoHelper.SEX, "");
        String height = mSharedPreferences.getString(UserInfoHelper.HEIGHT, "");
        String weight = mSharedPreferences.getString(UserInfoHelper.WEIGHT, "");
        return isInfoComplete() ? Util.getTarget(sex, height, weight) : 0;
    }
    
    public boolean saveInfo(String sex, String height, String weight, String age)
    {
        editor.putString(SEX, sex);
        editor.putString(HEIGHT, height);
        editor.putString(WEIGHT, weight);
        editor.putString(AGE, age);
        return editor.commit();
    }
    
    public String getSex()
    {
        return mSharedPreferences.getString(UserInfoHelper.SEX, "");
    }
    
    public String getHeight()
    {
        return mSharedPreferences.getString(UserInfoHelper.HEIGHT, "");
    }
    
    public String getWeight()
    {
        return mSharedPreferences.getString(UserInfoHelper.WEIGHT, "");
    }
    
    public String getAge()
    {
        return mSharedPreferences.getString(UserInfoHelper.AGE, "");
    }
}
